package admin

import (
	"context"
	v1 "learn-goframe/api/v1/admin"
	"learn-goframe/api/v1/common"
	"learn-goframe/internal/model"
	"learn-goframe/internal/service"
)

type cSolution struct{}

var Solution = cSolution{}

func (c *cSolution) SolutionList(ctx context.Context, req *v1.SolutionListReq) (res *v1.SolutionListRes, err error) {
	out, err := service.Solution().SolutionList(ctx, model.SolutionListInput{
		Page: req.Page,
		Size: req.Size,

		Status:        req.Status,
		SolutionName:  req.SolutionName,
		StatusExclude: 4, // 排查未提交的
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionListRes{
		List: out.List,
		PaginationRes: common.PaginationRes{
			Total: out.Total,
			Page:  out.Page,
			Size:  out.Size,
		},
	}, nil
}

// SolutionSave 保存解决方案
func (c *cSolution) SolutionSave(ctx context.Context, req *v1.SolutionUpdateReq) (res *v1.SolutionUpdateRes, err error) {
	out, err := service.Solution().SolutionAdminUpdate(ctx, model.SolutionAdminUpdateInput{
		Id:               req.Id,
		OrgName:          req.OrgName,
		OrgLicenseImgUrl: req.OrgLicenseImgUrl,
		ContactMobile:    req.ContactMobile,

		SolutionCreateBaseInput: model.SolutionCreateBaseInput{
			SolutionName:      req.SolutionName,
			SolutionImgUrl:    req.SolutionImgUrl,
			SolutionSummarize: req.SolutionSummarize,
			SolutionFileUrl:   req.SolutionFileUrl,
			CategoryId:        req.CategoryId,
			CategoryParentId:  req.CategoryParentId,
		},
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionUpdateRes{
		Id: out.Id,
	}, nil
}

// SolutionCheck 审核解决方案
func (c *cSolution) SolutionCheck(ctx context.Context, req *v1.SolutionCheckReq) (res *v1.SolutionCheckRes, err error) {
	err = service.Solution().SolutionCheck(ctx, model.SolutionCheckInput{
		Id:          req.Id,
		Status:      req.Status,
		CheckRemark: req.CheckRemark,
		CheckUserId: service.BizCtx().Get(ctx).User.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionCheckRes{}, nil
}

// SolutionDelete 删除解决方案
func (c *cSolution) SolutionDelete(ctx context.Context, req *v1.SolutionDeleteReq) (res *v1.SolutionDeleteRes, err error) {
	err = service.Solution().SolutionDelete(ctx, model.SolutionSingleIdInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionDeleteRes{}, nil
}
